﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using System.Collections;

namespace mdk 
{
    public class AnimationSprites
    {
        private List<Texture2D> aImagens;

        private int elapsedTime;

        private int time;

        private int index;

        private bool loop, finished;

        public AnimationSprites()
        {
            aImagens = new List<Texture2D>();
            finished = loop = false;
            
        }

        public void Add(Texture2D item)
        {
            aImagens.Add(item);
        }

        public void StartAnimation(int t)
        {
            time = t;
            elapsedTime = index = 0;
        }

        public void StartAnimation(int t, bool loop)
        {
            time = t;
            elapsedTime = index = 0;
            this.loop = loop;
        }

        public bool FinishAnimation()
        {
            return finished;
        }


        public Texture2D getImage(GameTime gameTime)
        {
            elapsedTime += gameTime.ElapsedGameTime.Milliseconds;

            if (elapsedTime >= time)
            {

                elapsedTime = 0;
                index++;
                if (index == aImagens.Count)
                {
                    if (loop)
                        index = 0;
                    else
                    {
                        index = aImagens.Count - 1;
                        finished = true;
                    }
                }


            }

            return aImagens[index];

        }

        public Texture2D getImage(int index)
        {
            if ((index < aImagens.Count) && (index >= 0))
                return aImagens[index];
            else
                return aImagens[0];
        }

        public int getHeight()
        {
            return aImagens[0].Height;
        }
        
        public int getWidth()
        {
            return aImagens[0].Width;
        }

    }
}
